<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>

</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>
<!--                <tr>-->
<!--                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>-->
<!--                    <td>1</td>-->
<!--                    <td>大秦帝国第一册</td>-->
<!--                    <td>我是作者</td>-->
<!--                    <td>23</td>-->
<!--                    <td>33.00</td>-->
<!--                    <td>北京出版社</td>-->
<!--                    <td>可借阅</td>-->
<!--                    <td>-->


<!--                        <div class="op">-->
<!--                            <a href="book_update.html?bookId=1">修改</a>-->
<!--                            <a href="javascript:void(0)" onclick="deleteBook(1)">删除</a>-->
<!--                        </div>-->


<!--                    </td>-->
<!--                </tr>-->

            </tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>
        <script>

            let totalCounts = 0;  // 总记录数
            let pageSize = 10;     // 每页的个数
            let visiblePages = 10; // 可视页数
            let currentPage = 1;   // 当前页码

            // 封装 AJAX 获取数据函数
            let getBookList = function (page) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "http://127.0.0.1:8080/books/getBooksByPage",
                        type: "get",
                        dataType: "json",
                        data: {
                            pageSize,
                            currentPage: page // 传入当前页码
                        },
                        success: function (books) {
                            console.log(books)
                            totalCounts = books.data.total; // 更新总记录数
                            let finalHtml = '';

                            for (let book of books.data.data) {
                                finalHtml += '<tr>';
                                finalHtml += '<td><input type="checkbox" name="selectBook" value="' + book.id + '" class="book-select"></td>';
                                finalHtml += '<td>' + book.id + '</td>';
                                finalHtml += '<td>' + book.bookName + '</td>';
                                finalHtml += '<td>' + book.author + '</td>';
                                finalHtml += '<td>' + book.count + '</td>';
                                finalHtml += '<td>' + book.price+ '</td>';
                                finalHtml += '<td>' + book.publish + '</td>';
                                finalHtml += '<td>' + book.statucCN + '</td>';
                                finalHtml += '<td>';
                                finalHtml += '<div class="op">';
                                finalHtml += '<a href="book_update.html?bookId=' + book.id + '">修改</a>';
                                finalHtml += '<a href="javascript:void(0)" onclick="deleteBook(' + book.id + ')">删除</a>';
                                finalHtml += '</div>';
                                finalHtml += '</td>';
                                finalHtml += '</tr>';
                            }

                            $('tbody').html(finalHtml); // 清空并更新表格内容
                            resolve(); // 数据加载完成，执行 resolve
                        },
                        error: function (err) {
                            if(err.status == 401)
                                location.href = "login.html";
                        }
                    });
                });
            };

            // 初始化分页插件
            async function initPagination() {
                $("#pageContainer").jqPaginator({
                    totalCounts: totalCounts, // 总记录数
                    pageSize: pageSize,
                    visiblePages: visiblePages,
                    currentPage: currentPage,
                    first: '<li class="page-item"><a class="page-link">首页</a></li>',
                    prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                    next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                    last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                    page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                    // 页面初始化和页码点击时都会执行
                    onPageChange: async function (page, type) {
                        console.log("第" + page + "页, 类型:" + type);
                        await getBookList(page); // 获取数据并更新表格
                    }
                });
            }

            // 先获取第一页数据，然后初始化分页插件
            async function init() {
                await getBookList(1);  // 先加载第一页的数据
                initPagination();       // 再初始化分页组件
            }

            // 执行初始化
            init();


            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    $.ajax({
                        url: "http://127.0.0.1:8080/books/deleteBook",
                        type: "get",
                        dataType: "json",
                        data: {
                            id: id
                        },
                        success: function (data) {
                            // alert("删除成功");
                        },
                        error: function (err) {
                            // alert("删除失败");
                        }
                    });

                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    $.ajax({
                        url: "http://127.0.0.1:8080/books/batchDeleteBook",
                        type: "get",
                        dataType: "json",
                        data: {
                            ids: ids
                        },
                        success: function (data) {
                            // alert("批量删除成功");
                        },
                        error: function (err) {
                            // alert("批量删除失败");
                        }
                    });
                }
            }



        </script>
    </div>
</body>

</html>